<?php
require_once "common.php";
?><html>
	<head>
		<style type="text/css">
			#conash3D0{display:none;}
			body,form,p,input,textarea,label{
				font-size: 12px;
			}
			body{
				padding:5px;
				margin:0;
			}
		</style>
	</head>
	<body><?php
if(!isLogin()){
	die('Login required.');
}

function extfilename($file_name,$pass_type=array('jpg','jpeg','gif','png')){ 
	$yx_file = $pass_type; 
	$kzm = substr(strrchr($file_name,"."),1); 
	return  in_array(strtolower($kzm),$yx_file); 
}

if(isset($_POST['submit'])){
	if(!in_array($_FILES['image']['type'],array('image/jpeg', 'image/gif', 'image/png', 'image/pjpeg', 'image/x-png')) || !extfilename($_FILES['image']['name'])){
		die('Please upload image files');
	}
	if($_FILES["image"]["size"]>1024*256){
		die('File size is too large.');
	}

	$filename = 'tmp/'.md5(time()).$_FILES['image']['name'];
	move_uploaded_file($_FILES['image']['tmp_name'],$filename);

	$request = $_POST['img'] == 'avatar' ? "http://api.twitter.com/1/account/update_profile_image.json" : "http://api.twitter.com/1/account/update_profile_background_image.json";
	$file = array(
		'image' => '@'.$filename
	);
	$result = json_decode(oAuthRequest($request, "POST", $file, true));
	if(!$result || $result->error){
		echo 'Error';
	}
	else{
		echo 'Successful.';
?><script type="text/javascript">
window.parent.loaduserinfo();
window.parent.alert('Upload successfully. If the background or your avatar doesn\'t change, please refresh kwestion after several minutes.');
</script><?php
}
unlink($filename);
}
?>

		<form method="post" enctype="multipart/form-data">
			<input type="radio" name="img" value="background" checked>Background 
			<input type="radio" name="img" value="avatar">Avatar<br />

			<label for="file">Image:</label>
			<input type="file" name="image" id="image" /><br />
			<input type="submit" name="submit" value="Upload" />
		</form>
	</body>
</html>

